<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class CreatePlayListRelationTable extends AbstractMigration
{
    public function up()
    {
        $this->table('review_request')
            ->addColumn('need_handle', 'enum', ['values' => 'yes,no', 'null' => false, 'default' => 'no', 'comment' => '是否需要组装处理'])
            ->addIndex(['module_id', 'link_id'], ['type' => 'normal', 'name' => 'idx_module_id_link_id'])
            ->save();

        $this->table('review_request_node')
            ->addIndex(['review_request_id'], ['type' => 'normal', 'name' => 'idx_review_request_id'])
            ->save();

        $this->table('version')
            ->addColumn('is_second_submit', 'enum', ['values' => 'yes,no', 'null' => false, 'default' => 'no', 'comment' => '是否二阶段提交'])
            ->addIndex(['module_id', 'link_id'], ['type' => 'normal', 'name' => 'idx_module_id_link_id'])
            ->save();

        $this->table('playlist_package', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '播放列表包'])
            ->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => '主键ID'])
            ->addColumn('name', 'string', ['null' => false, 'default' => '', 'limit' => 255, 'comment' => '播放列表名称'])
            ->addColumn('initial_episode_code', 'string', ['null' => false, 'default' => '', 'limit' => 20, 'comment' => '集code'])
            ->addColumn('showings_number_str', 'string', ['null' => false, 'default' => '', 'limit' => 512, 'comment' => '场次str'])
            ->addColumn('module_code', 'string', ['null' => false, 'default' => '', 'limit' => 40, 'comment' => '模块code'])
            ->addColumn('plan_end_time', 'integer', ['null' => false, 'limit' => 11, 'default' => 0, 'signed' => false, 'comment' => '审核截至时间'])
            ->addColumn('tenant_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '租户id'])
            ->addColumn('project_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '项目ID'])
            ->addColumn('create_type', 'enum', ['null' => false, 'values' => 'assemble_list,order', 'default' => 'assemble_list', 'comment' => '创建类型'])
            ->addColumn('order_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '订单id'])
            ->addColumn('created_by', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建人',])
            ->addColumn('created', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建时间',])
            ->addColumn('updated', 'integer', ['null' => false, 'limit' => 11, 'default' => 0,])
            ->save();
        $this->table('playlist_package_item', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '播放列表包-播放项'])
            ->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => '主键ID'])
            ->addColumn('playlist_package_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '播放列表id'])
            ->addColumn('entity_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '实体id'])
            ->addColumn('sort_index', 'integer', ['default' => 0, 'limit' => 11, 'comment' => '排序 ASC'])
            ->addColumn('created', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建时间',])
            ->addColumn('created_by', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建人',])
            ->save();


        $this->table('playlist', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '播放列表(状态存储row)'])
            ->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => '主键ID'])
            ->addColumn('name', 'string', ['null' => false, 'default' => '', 'limit' => 255, 'comment' => '播放列表名称'])
            ->addColumn('current_number', 'string', ['null' => false, 'default' => '', 'limit' => 20, 'comment' => '当前版本号'])
            ->addColumn('initial_episode_code', 'string', ['null' => false, 'default' => '', 'limit' => 20, 'comment' => '集code'])
            ->addColumn('showings_number_str', 'string', ['null' => false, 'default' => '', 'limit' => 512, 'comment' => '场次str'])
            ->addColumn('module_code', 'string', ['null' => false, 'default' => '', 'limit' => 40, 'comment' => '模块code'])
            ->addColumn('playlist_package_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '播放列表包id'])
            ->addColumn('plan_end_time', 'integer', ['null' => false, 'limit' => 11, 'default' => 0, 'signed' => false, 'comment' => '审核截至时间'])
            ->addColumn('tenant_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '租户id'])
            ->addColumn('project_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '项目ID'])
            ->addColumn('step_category_id', 'integer', ['signed' => false, 'null' => false, 'default' => 0, 'limit' => 11, 'comment' => '工序分类ID'])
            ->addColumn('step_id', 'integer', ['signed' => false, 'null' => false, 'default' => 0, 'limit' => 11, 'comment' => '工序ID'])
            ->addColumn('latest_playlist_version_id', 'integer', ['signed' => false, 'null' => false, 'default' => 0, 'limit' => 11, 'comment' => '最后一个版本id'])
            ->addColumn('created_by', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建人',])
            ->addColumn('created', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建时间',])
            ->addColumn('updated', 'integer', ['null' => false, 'limit' => 11, 'default' => 0,])
            ->addColumn('status', 'enum', ['values' => 'not_done,wait_submit,wait_review,review_done', 'default' => 'not_done', 'null' => false, 'comment' => '审核状态'])
            ->save();
        $this->table('playlist_item', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '播放列表-播放项'])
            ->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => '主键ID'])
            ->addColumn('playlist_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '播放列表id'])
            ->addColumn('entity_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '实体id'])
            ->addColumn('task_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '任务id'])
            ->addColumn('sort_index', 'integer', ['default' => 0, 'limit' => 11, 'comment' => '排序 ASC'])
            ->addColumn('created', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建时间',])
            ->addColumn('created_by', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建人',])
            ->addColumn('review_role_code', 'string', ['null' => false, 'default' => '', 'limit' => 40, 'comment' => '当前审核人角色code'])
            ->save();

        $this->table('playlist_version', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '播放列表-版本表'])
            ->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => '主键ID'])
            ->addColumn('name', 'string', ['null' => false, 'default' => '', 'limit' => 255, 'comment' => '播放列表名称'])
            ->addColumn('number', 'string', ['null' => false, 'default' => '', 'limit' => 20, 'comment' => '版本号'])
            ->addColumn('initial_episode_code', 'string', ['null' => false, 'default' => '', 'limit' => 20, 'comment' => '集code'])
            ->addColumn('showings_number_str', 'string', ['null' => false, 'default' => '', 'limit' => 512, 'comment' => '场次str'])
            ->addColumn('module_code', 'string', ['null' => false, 'default' => '', 'limit' => 40, 'comment' => '模块code'])
            ->addColumn('current_review_code', 'string', ['null' => false, 'default' => '', 'limit' => 20, 'comment' => '当前审核环节角色code'])
            ->addColumn('playlist_id', 'integer', ['signed' => false, 'default' => 0, 'limit' => 11, 'comment' => '播放列表id'])
            ->addColumn('playlist_package_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '播放列表包id'])
            ->addColumn('step_category_id', 'integer', ['signed' => false, 'null' => false, 'default' => 0, 'limit' => 11, 'comment' => '工序分类ID'])
            ->addColumn('step_id', 'integer', ['signed' => false, 'null' => false, 'default' => 0, 'limit' => 11, 'comment' => '工序ID'])
            ->addColumn('tenant_id', 'integer', ['signed' => false, 'null' => false, 'default' => 0, 'limit' => 11, 'comment' => '租户ID'])
            ->addColumn('project_id', 'integer', ['signed' => false, 'null' => false, 'default' => 0, 'limit' => 11, 'comment' => '项目ID'])
            ->addColumn('plan_end_time', 'integer', ['null' => false, 'limit' => 11, 'default' => 0, 'signed' => false, 'comment' => '审核截至时间'])
            ->addColumn('created_by', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建人',])
            ->addColumn('created', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建时间',])
            ->addColumn('updated', 'integer', ['null' => false, 'limit' => 11, 'default' => 0,])
            ->addColumn('status', 'enum', ['values' => 'wait_review,reviewing,review_done', 'null' => false, 'default' => 'wait_review', 'comment' => '审核状态'])
            ->save();


        $this->table('playlist_item_version', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '播放列表-播放项版本'])
            ->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => '主键ID'])
            ->addColumn('current_review_code', 'string', ['null' => false, 'default' => '', 'limit' => 20, 'comment' => '当前审核环节角色code'])
            ->addColumn('playlist_version_id', 'integer', ['signed' => false, 'limit' => 11, 'comment' => '播放列表-版本id'])
            ->addColumn('playlist_package_id', 'integer', ['signed' => false, 'limit' => 11, 'default' => 0, 'comment' => '播放列表包id'])
            ->addColumn('playlist_id', 'integer', ['signed' => false, 'limit' => 11, 'comment' => '播放列表-版本id'])
            ->addColumn('playlist_item_id', 'integer', ['signed' => false, 'limit' => 11, 'comment' => '播放列表-播放项id'])
            ->addColumn('entity_id', 'integer', ['signed' => false, 'limit' => 11, 'comment' => '实体id'])
            ->addColumn('review_request_id', 'integer', ['default' => 0, 'null' => false, 'signed' => false, 'limit' => 11, 'comment' => '审核申请id'])
            ->addColumn('version_id', 'integer', ['default' => 0, 'null' => false, 'signed' => false, 'limit' => 11, 'comment' => '任务版本id'])
            ->addColumn('task_id', 'integer', ['signed' => false, 'limit' => 11, 'comment' => '任务id'])
            ->addColumn('sort_index', 'integer', ['default' => 0, 'limit' => 11, 'comment' => '排序 ASC'])
            ->addColumn('status', 'enum', ['values' => 'wait_review,reviewing,review_done', 'default' => 'wait_review', 'null' => false, 'comment' => '审核状态'])
            ->addColumn('created', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建时间',])
            ->addColumn('created_by', 'integer', ['null' => false, 'default' => 0, 'limit' => 11, 'signed' => false, 'comment' => '创建人',])
            ->save();


    }

    public function down()
    {
        $this->table('review_request')->removeColumn('need_handle')->removeIndexByName('idx_module_id_link_id')->save();
        $this->table('review_request_node')->removeIndexByName('idx_review_request_id')->save();
        $this->table('version')->removeIndexByName('idx_module_id_link_id')->save();
        $this->table('playlist_package')->drop()->save();
        $this->table('playlist_package_item')->drop()->save();
        $this->table('playlist')->drop()->save();
        $this->table('playlist_item')->drop()->save();
        $this->table('playlist_version')->drop()->save();
        $this->table('playlist_item_version')->drop()->save();
    }
}
